/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.moltos.rabbit;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;

/**
 *
 * @author MoLt1eS
 */
public class Stuff {
    
    public String DoStuff(String bodyParameters){
        StringBuilder output = new StringBuilder();
        
        String answer = null;
        
        try {
            

            // Tell the URLConnection to use a SocketFactory from our SSLContext
            URL url = new URL("https://");
            HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
            
            urlConnection.setHostnameVerifier(new HostnameVerifier() {

                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                            
                            return true;
                    }
            });
            
            urlConnection.setSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());

            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST"); 
            urlConnection.setRequestProperty("Content-Type", "application/json"); 
            urlConnection.setRequestProperty("charset", "utf-8");
            urlConnection.setRequestProperty("Content-Length", "" + Integer.toString(bodyParameters.getBytes().length));

            urlConnection.connect();

            DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream ());
            wr.writeBytes(bodyParameters);
            wr.flush();
            wr.close();

            InputStream is;
            if (urlConnection.getResponseCode() == 200) {
                
                is = urlConnection.getInputStream();
                
                /* obtém a resposta do pedido */
                int n = 1;	
                while (n > 0) {
	
                    byte[] b = new byte[4096];

                    n =  is.read(b);
	
                    if (n > 0)
                        output.append(new String(b, 0, n));
                }
                
                /* instancia um JSON Parser */
                
                /* constrói um JSON Object relativo com a resposta */
                answer = output.toString();
                
            } 
            else {
                System.out.println(urlConnection.getResponseCode());
            }

            urlConnection.disconnect();
        }
        catch (IOException ex) {
            
            ex.printStackTrace();
        }
        
        return answer;
    }
}
